/*
*  events.c - MSN Events
*  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "events.h"
#include "string.h"

MsnEventQueue *
msn_event_queue_new (void)
{
	MsnEventQueue *equeue;

	equeue = g_new (MsnEventQueue, 1);
	equeue->queue = g_queue_new ();
	equeue->length = 0;

	return equeue;
}

void
msn_event_queue_free (MsnEventQueue *equeue)
{
	MsnEvent *event = NULL;

	g_return_if_fail (equeue != NULL);
	g_return_if_fail (equeue->queue != NULL);

	while (event = msn_get_event (equeue))
		msn_event_free (event);

	g_queue_free (equeue->queue);
	g_free (equeue);

	return;
}

MsnEvent *
msn_event_new (char *event_name, gpointer data)
{
	MsnEvent *event = NULL;
	MsnEventID id = INVALID_EVENT;

	g_return_val_if_fail (event_name != NULL, NULL);

	event = g_new (MsnEvent, 1);

	if (strcmp (event_name, "contact_added") == 0)
		id = CONTACT_ADDED;
	else if (strcmp (event_name, "contact_info_changed") == 0)
		id = CONTACT_INFO_CHANGED;
	else if (strcmp (event_name, "group_added") == 0)
		id = GROUP_ADDED;
	else if (strcmp (event_name, "group_removed") == 0)
		id = GROUP_REMOVED;
	else if (strcmp (event_name, "sync_finished") == 0)
		id = SYNC_FINISHED;
	else if (strcmp (event_name, "switchboard_invited") == 0)
		id = SWITCHBOARD_INVITED;
	else if (strcmp (event_name, "user_nickname_changed") == 0)
		id = USER_NICKNAME_CHANGED;
	else if (strcmp (event_name, "user_status_changed") == 0)
		id = USER_STATUS_CHANGED;

	event->name = g_strdup (event_name);
	event->user_data = data;
	event->id = id;

	return event;
}

void 
msn_event_free (MsnEvent *event)
{
	g_return_if_fail (event != NULL);

	/* NOTE: The event handler should free the event specific data */

	g_free (event->name);
	event->id = INVALID_EVENT;

	g_free (event);

	return;
}

int
msn_event_add (MsnEventQueue *equeue,
	       char *event_name,
	       gpointer event_data)
{
	MsnEvent *event = NULL;

	g_return_val_if_fail (equeue != NULL, -1);
	g_return_val_if_fail (equeue->queue != NULL, -1);
	g_return_val_if_fail (event_name != NULL, -1);

	event = msn_event_new (event_name, event_data);

	if (event == NULL)
		return -1;

	g_queue_push_tail (equeue->queue, event);
	equeue->length = g_queue_get_length (equeue->queue);

	return 0;
}

MsnEvent *
msn_get_event (MsnEventQueue *equeue)
{
	MsnEvent *event = NULL;

	g_return_val_if_fail (equeue != NULL, NULL);
	g_return_val_if_fail (equeue->queue != NULL, NULL);

	if (equeue->length == 0)
		return NULL;

	event = (MsnEvent *) g_queue_pop_head (equeue->queue);
	equeue->length = g_queue_get_length (equeue->queue);

	return event;
}

gboolean
msn_event_available (MsnEventQueue *equeue)
{
	return (equeue->length > 0);
}
